﻿using System;
using System.Management;

namespace TSharp.Core.Env
{
    /// <summary>
    /// 计算机信息
    /// </summary>
    public class Computer
    {
        private static string _CpuID;
        private static string _MacAddress;
        private static string _DiskID;
        private static string _SystemType;
        private static string _TotalPhysicalMemory; //单位：M   
        private static string _ComputerName;
        private static string _IpAddress;
        private static string _LoginUserName;

        static Computer()
        {
            ReRead();
        }

        /// <summary>
        /// CPU id
        /// </summary>
        public static string CpuID
        {
            get { return _CpuID; }
        }

        /// <summary>
        /// Mac地址
        /// </summary>
        public static string MacAddress
        {
            get { return _MacAddress; }
            //set { Computer._MacAddress = value; }
        }

        /// <summary>
        /// 硬盘号
        /// </summary>
        public static string DiskID
        {
            get { return _DiskID; }
        }

        /// <summary>
        /// IP
        /// </summary>
        public static string IpAddress
        {
            get { return _IpAddress; }
        }

        /// <summary>
        /// 登录用户名
        /// </summary>
        public static string LoginUserName
        {
            get { return _LoginUserName; }
            //set { Computer._LoginUserName = value; }
        }

        /// <summary>
        /// 计算机名称
        /// </summary>
        public static string ComputerName
        {
            get { return _ComputerName; }
        }

        /// <summary>
        /// 系统类型
        /// </summary>
        public static string SystemType
        {
            get { return _SystemType; }
        }

        /// <summary>
        /// 内存
        /// </summary>
        public static string TotalPhysicalMemory
        {
            get { return _TotalPhysicalMemory; }
        }

        /// <summary>
        /// 重读系统
        /// </summary>
        public static void ReRead()
        {
            _CpuID = GetCpuID();
            _MacAddress = GetMacAddress();
            _DiskID = GetDiskID();
            _IpAddress = GetIPAddress();
            _LoginUserName = GetUserName();
            _SystemType = GetSystemType();
            _TotalPhysicalMemory = GetTotalPhysicalMemory();
            _ComputerName = GetComputerName();
        }

        private static string GetCpuID()
        {
            try
            {
                //获取CPU序列号代码   
                string cpuInfo = ""; //cpu序列号   
                var mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        private static string GetMacAddress()
        {
            try
            {
                //获取网卡硬件地址   
                string mac = "";
                var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"])
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        private static string GetIPAddress()
        {
            try
            {
                //获取IP地址   
                string st = "";
                var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"])
                    {
                        //st=mo["IpAddress"].ToString();   
                        Array ar;
                        ar = (Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        private static string GetDiskID()
        {
            try
            {
                //获取硬盘ID   
                String HDid = "";
                var mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        /// <summary>   
        /// 操作系统的登录用户名   
        /// </summary>   
        /// <returns></returns>   
        private static string GetUserName()
        {
            try
            {
                string st = "";
                var mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = (string)mo["UserName"];
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        /// <summary>   
        /// PC类型   
        /// </summary>   
        /// <returns></returns>   
        private static string GetSystemType()
        {
            try
            {
                string st = "";
                var mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["SystemType"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        /// <summary>   
        /// 物理内存   
        /// </summary>   
        /// <returns></returns>   
        private static string GetTotalPhysicalMemory()
        {
            try
            {
                string st = "";
                var mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["TotalPhysicalMemory"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        /// <summary>   
        ///    
        /// </summary>   
        /// <returns></returns>   
        private static string GetComputerName()
        {
            try
            {
                return global::System.Environment.GetEnvironmentVariable("ComputerName");
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }
    }
}